
package com.mea.site.module.sys.model;

import com.mea.site.common.base.model.DataEntity;
import com.mea.site.common.utils.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.Map;

/**
 * 日志Entity
 * @author Michael
 */
@Table(name = "sys_log")
public class Log extends DataEntity<Log> {

	private static final long serialVersionUID = 1L;

	@Column(name = "operation")
	private String operation;		// 日志标题
	@Column(name = "remote_addr")
	private String remoteAddr; 	// 操作用户的IP地址
	@Column(name = "request_uri")
	private String requestUri; 	// 操作的URI
	@Column(name = "method")
	private String method; 		// 操作的方式
	@Column(name = "params")
	private String params; 		// 操作提交的数据
	@Column(name = "user_agent")
	private String userAgent;	// 操作用户代理信息
	@Column(name = "method_name")
	private String methodName; //方法名称
	@Transient
	private Date beginDate;		// 开始日期
	@Transient
	private Date endDate;		// 结束日期
	@Column(name = "exception")
	private String exception;

	/**
	 * 响应时间
	 */
	@Column(name = "response_time")
	private long responseTime;
	/**
	 * 请求时间
	 */
	@Column(name = "request_time")
	private long requestTime;
	@Column(name = "time")
	private Integer time;

	
	public Log(){
		super();
	}
	
	public Log(String id){
		super(id);
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public String getRequestUri() {
		return requestUri;
	}

	public void setRequestUri(String requestUri) {
		this.requestUri = requestUri;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}


	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public Integer getTime() {
		return time;
	}

	public void setTime(Integer time) {
		this.time = time;
	}

	/**
	 * 设置请求参数
	 * @param paramMap
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setParams(Map paramMap){
		if (paramMap == null){
			return;
		}
		StringBuilder params = new StringBuilder();
		for (Map.Entry<String, String[]> param : ((Map<String, String[]>)paramMap).entrySet()){
			params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
			String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
			params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
		}
		this.params = params.toString();
	}
	
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}

	public long getResponseTime() {
		return responseTime;
	}

	public void setResponseTime(long responseTime) {
		this.responseTime = responseTime;
	}

	public long getRequestTime() {
		return requestTime;
	}

	public void setRequestTime(long requestTime) {
		this.requestTime = requestTime;
	}

	public String getException() {
		return exception;
	}

	public void setException(String exception) {
		this.exception = exception;
	}
}